package com.z.controller;

import com.z.pojo.Users;
import com.z.service.UsersService;
import com.z.service.impl.UsersServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @author 张福兴
 * date 2021/9/9
 */
@WebServlet("/usersLogin")
public class UserLoginServlet extends HttpServlet {
    private final UsersService usersService = new UsersServiceImpl();

    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String referer = (String) req.getSession().getAttribute("referer");

        String account = req.getParameter("account");
        String password = req.getParameter("password");

        String checkcode = req.getParameter("checkcode");
        String safeCode = (String) req.getSession().getAttribute("safeCode");

        if (!checkcode.equalsIgnoreCase(safeCode)){
            req.setAttribute("msg", "验证码错误");
            req.getRequestDispatcher("/login.jsp").forward(req, resp);
        }

        Users users = usersService.usersLogin(account,password);
        if (users == null) {
            req.setAttribute("msg", "账号或密码错误");
            req.getRequestDispatcher("/login.jsp").forward(req, resp);
        }
        req.getSession().setAttribute("users",users);
        resp.sendRedirect(referer);

    }
}
